//
//  RWTabBarController.m
//  baisibudejie
//
//  Created by renwei on 16/9/13.
//  Copyright © 2016年 renwei. All rights reserved.
//

#import "RWTabBarController.h"

#import "RWEssenceViewController.h"
#import "RWFriendTrendViewController.h"
#import "RWMeViewController.h"
#import "RWNewViewController.h"
#import "RWPublishViewController.h"

#import "UIImage+Image.h"
#import "RWTabBar.h"
#import "RWNavigationViewController.h"

@interface RWTabBarController()

@end

@implementation RWTabBarController

/*
 问题:
 1.选中按钮的图片被渲染 -> iOS7之后默认tabBar上按钮图片都会被渲染 1.修改图片 2.通过代码 √
 2.选中按钮的标题颜色:黑色 标题字体大 -> 对应子控制器的tabBarItem
 3.发布按钮显示不出来
 */

// 只会调用一次
+ (void)load {
    
    // 获取哪个类中UITabBarItem
    UITabBarItem *item = [UITabBarItem appearanceWhenContainedIn:self, nil];
    
    // 设置按钮选中标题的颜色:富文本:描述一个文字颜色,字体,阴影,空心,图文混排
    // 创建一个描述文本属性的字典
    NSMutableDictionary *attrs = [NSMutableDictionary dictionary];
    attrs[NSForegroundColorAttributeName] = [UIColor blackColor];
    [item setTitleTextAttributes:attrs forState:UIControlStateSelected];
    
    // 设置字体尺寸:只有设置正常状态下,才会有效果
    NSMutableDictionary *attrsNor = [NSMutableDictionary dictionary];
    attrsNor[NSFontAttributeName] = [UIFont systemFontOfSize:13];
    [item setTitleTextAttributes:attrsNor forState:UIControlStateNormal];
}

#pragma mark - 生命周期方法
- (void)viewDidLoad {
    [super viewDidLoad];
    
    // Do any additional setup after loading the view.
    // 1 添加子控制器(5个子控制器) -> 自定义控制器 -> 划分项目文件结构
    [self setupAllChildViewController];
    
    // 2 设置tabBar上按钮内容 -> 由对应的子控制器的tabBarItem属性
    [self setupAllTitleButton];
    
    // 3 自定义tabBar
    [self setupTabBar];
    
    RWLog(@"123456");
}

#pragma mark - 自定义tabBar
- (void)setupTabBar {

    RWTabBar *tabBar = [[RWTabBar alloc] init];
    
    [self setValue:tabBar forKey:@"tabBar"];
}

#pragma mark - 添加所有子控制器
- (void)setupAllChildViewController {
    
    // 精华
    RWEssenceViewController *essenceVc = [[RWEssenceViewController alloc] init];
    RWNavigationViewController *nav = [[RWNavigationViewController alloc] initWithRootViewController:essenceVc];
    // initWithRootViewController:push
    [self addChildViewController:nav];
    
    // 新帖
    RWNewViewController *newVc = [[RWNewViewController alloc] init];
    RWNavigationViewController *nav1 = [[RWNavigationViewController alloc] initWithRootViewController:newVc];
    [self addChildViewController:nav1];
    
    // 关注
    RWFriendTrendViewController *ftVc = [[RWFriendTrendViewController alloc] init];
    RWNavigationViewController *nav3 = [[RWNavigationViewController alloc] initWithRootViewController:ftVc];
    [self addChildViewController:nav3];
    
    // 我
    RWMeViewController *meVc = [[RWMeViewController alloc] init];
    RWNavigationViewController *nav4 = [[RWNavigationViewController alloc] initWithRootViewController:meVc];
    [self addChildViewController:nav4];
}

// 设置tabBar上所有按钮内容
- (void)setupAllTitleButton {
    
    // 0:nav
    RWNavigationViewController *nav = self.childViewControllers[0];
    nav.tabBarItem.title = @"精华";
    nav.tabBarItem.image = [UIImage imageNamed:@"tabBar_essence_icon"];
    // 快速生成一个没有渲染图片
    nav.tabBarItem.selectedImage = [UIImage imageOriginalWithName:@"tabBar_essence_click_icon"];
    
    // 1:新帖
    RWNavigationViewController *nav1 = self.childViewControllers[1];
    nav1.tabBarItem.title = @"新帖";
    nav1.tabBarItem.image = [UIImage imageNamed:@"tabBar_new_icon"];
    nav1.tabBarItem.selectedImage = [UIImage imageOriginalWithName:@"tabBar_new_click_icon"];
    
    
    // 3.关注
    RWNavigationViewController *nav3 = self.childViewControllers[2];
    nav3.tabBarItem.title = @"关注";
    nav3.tabBarItem.image = [UIImage imageNamed:@"tabBar_friendTrends_icon"];
    nav3.tabBarItem.selectedImage = [UIImage imageOriginalWithName:@"tabBar_friendTrends_click_icon"];
    
    // 4.我
    RWNavigationViewController *nav4 = self.childViewControllers[3];
    nav4.tabBarItem.title = @"我";
    nav4.tabBarItem.image = [UIImage imageNamed:@"tabBar_me_icon"];
    nav4.tabBarItem.selectedImage = [UIImage imageOriginalWithName:@"tabBar_me_click_icon"];

}

@end
